import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

import 'lf_product_plugin_platform_interface.dart';

/// An implementation of [LfProductPluginPlatform] that uses method channels.
class MethodChannelLfProductPlugin extends LfProductPluginPlatform {
  /// The method channel used to interact with the native platform.
  @visibleForTesting
  final methodChannel = const MethodChannel('lf_product_plugin');

  final eventChannel = const EventChannel('lf_product_plugin_event');

  /// 订阅 - 解决  eventChannel 重复订阅的问题
  StreamSubscription? _subscription;

  /// 监听
  void Function(dynamic event)? onData;

  void cancelListening() {
    // debugPrint("===== 订阅取消 ==== ");
    _subscription?.cancel();
    _subscription = null;
  }

  void onListening(void Function(dynamic event) onData) {
    _subscription?.cancel();
    _subscription = eventChannel.receiveBroadcastStream().listen((event) async {
      if (event is! Map) {
        print('------- lf_product_plugin_method_channel -----------');
        print(event);
        print("event 类型有误，当前类型: ${event.runtimeType}");
        return;
      }
      // 执行回调
      onData(event);
    });
  }

  @override
  Future<String?> getPlatformVersion() async {
    final version =
        await methodChannel.invokeMethod<String>('getPlatformVersion');
    return version;
  }

  @override
  Future<String?> getHelloWorld() async {
    final version = await methodChannel.invokeMethod<String>('getHelloWorld');
    return version;
  }

  @override
  Future<dynamic> calculate4({
    required double height,
    required double weight,
    required double heartRate,
    required double impendance,
    required int age,
    required int gender, // 1=男, 0=女
  }) async {
    double tempImpendance = impendance;
    if (tempImpendance > 1000) {
      tempImpendance = 1000;
    }
    if (tempImpendance < 300) {
      tempImpendance = 300;
    }
    final version = await methodChannel.invokeMethod<dynamic>(
      'calculate4',
      {
        'height': height,
        'weight': weight,
        'heartRate': heartRate,
        'impendance': tempImpendance,
        'age': age,
        'gender': gender,
      },
    );
    return version;
  }

  @override
  Future<dynamic> calculate8({
    required double height,
    required double weight,
    required double heartRate,
    required int age,
    required int gender, // 1=男, 0=女
    required int z20KhzLeftArmEnCode,
    required int z20KhzRightArmEnCode,
    required int z20KhzLeftLegEnCode,
    required int z20KhzRightLegEnCode,
    required int z20KhzTrunkEnCode,
    required int z100KhzLeftArmEnCode,
    required int z100KhzRightArmEnCode,
    required int z100KhzLeftLegEnCode,
    required int z100KhzRightLegEnCode,
    required int z100KhzTrunkEnCode,
  }) async {
    final version = await methodChannel.invokeMethod<dynamic>(
      'calculate8',
      {
        'height': height,
        'weight': weight,
        'heartRate': heartRate,
        'age': age,
        'gender': gender,
        'z20KhzLeftArmEnCode': z20KhzLeftArmEnCode,
        'z20KhzRightArmEnCode': z20KhzRightArmEnCode,
        'z20KhzLeftLegEnCode': z20KhzLeftLegEnCode,
        'z20KhzRightLegEnCode': z20KhzRightLegEnCode,
        'z20KhzTrunkEnCode': z20KhzTrunkEnCode,
        'z100KhzLeftArmEnCode': z100KhzLeftArmEnCode,
        'z100KhzRightArmEnCode': z100KhzRightArmEnCode,
        'z100KhzLeftLegEnCode': z100KhzLeftLegEnCode,
        'z100KhzRightLegEnCode': z100KhzRightLegEnCode,
        'z100KhzTrunkEnCode': z100KhzTrunkEnCode,
      },
    );
    return version;
  }

  @override
  Future<String?> scan() async {
    final version = await methodChannel.invokeMethod<String>('scan');
    return version;
  }

  @override
  Future<String?> link({
    required dynamic device,
  }) async {
    final version =
        await methodChannel.invokeMethod<String>('link', {'device': device});
    return version;
  }
}
